package com.googlecode.agscrum.model.entity;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

import org.hibernate.annotations.Cascade;

@Entity
@SequenceGenerator(name = "role_sequence", sequenceName = "role_sequence")
public class Role {
	@Id
	@GeneratedValue(generator = "role_sequence", strategy = GenerationType.SEQUENCE)
	@Column(name = "id_role")
	private Long id;
	@Column(length = 70, nullable = false, name = "AUTHORITY")
	private String description;
	@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@Cascade(value = { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	@JoinColumn(name = "id_role")
	private Set<RoleResourceAction> roleActions;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
